package servlet;

import com.google.gson.Gson;
import dao.impl.NewsDaoImpl;
import entity.News;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/NewsServlet")
public class NewsServlet extends HttpServlet {
    PrintWriter pw;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        pw = resp.getWriter();
        String operation = req.getParameter("operation");
        if(operation.equals("insert")){
            insertNews(req);
        }
        if(operation.equals("findAll")){
            findAllNews();
        }
        if(operation.equals("findLikes")){
            findLikesNews(req);
        }
        if(operation.equals("findNewsByName")){
            findNewsByName(req);
        }
    }



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    /*
    *   Android到Servlet有中文字符乱码问题
    *   在安卓客户端将String中的汉字由UTF-8转码为ISO8859-1.
    *   在servlet服务端将String中的汉字由ISO8859-1转码为UTF-8.
    */
    private void insertNews(HttpServletRequest req) {
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        String createTime = req.getParameter("createTime");
        String author = req.getParameter("author");
        try {
            title = new String(title.getBytes("iso8859-1"),"UTF-8");
            content = new String(content.getBytes("iso8859-1"),"UTF-8");
            author = new String(author.getBytes("iso8859-1"),"UTF-8");
            News news = new News(title,content,author,createTime);
            int result = new NewsDaoImpl().insertNews(news);
            System.out.println(result+"");
            pw.write(result+"");
            pw.flush();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void findNewsByName(HttpServletRequest req) {
        String author = req.getParameter("username");
        List<News> list = null;
        try {
            list = new NewsDaoImpl().findNewsByName(author);
            Gson gson = new Gson();
            String JsonNews = gson.toJson(list);
            System.out.println(JsonNews);
            pw.write(JsonNews);
            pw.flush();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    private void findAllNews() {
        try {
            List<News> list = new NewsDaoImpl().findAllNews();
            Gson gson = new Gson();
            String JsonNews = gson.toJson(list);
            System.out.println(JsonNews);
            pw.write(JsonNews);
            pw.flush();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void findLikesNews(HttpServletRequest req) {
        String args = req.getParameter("args");
        Gson gson;
        try {
            args = new String(args.getBytes("iso8859-1"),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(args);
        try {
            List<News> list = new NewsDaoImpl().findLikes(args);
            if(list.size()>0){
                gson = new Gson();
                String resultJson = gson.toJson(list);
                pw.write(resultJson);
                pw.flush();
                return;
            }else{
                pw.write("0");
                pw.flush();
            }
            System.out.println(list.size());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
